local zhuini = fk.CreateSkill{
  name = "hanqing__zhuini",
}

Fk:loadTranslationTable{
  ["hanqing__zhuini"] = "惴逆",
  [":hanqing__zhuini"] = "当你指定或成为【杀】的唯一目标后，使用者可以展示目标角色(1)张牌，令其选择一项：1.将此牌交给使用者；"..
  "2.弃置此牌并重铸其余牌，然后其可以令你转换势力。",

  ["#hanqing__zhuini-invoke"] = "惴逆：是否展示 %dest 的%arg张牌？",
  ["#hanqing__zhuini-show"] = "惴逆：展示 %dest 的%arg张牌",
  ["#hanqing__zhuini-discard"] = "惴逆：点“确定”将展示的牌交给 %dest，或点“取消”弃置此牌并重铸其余的牌",
  ["#hanqing__zhuini-change"] = "惴逆：是否令 %dest 转换势力？",

  ["$hanqing__zhuini1"] = "君子负大勇，量敌而后进，岂能曰畏？",
  ["$hanqing__zhuini2"] = "四时则生乱气，今天下形势，当从有道。",
}

zhuini:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuini.name) and
      (event == fk.TargetSpecified and data.firstTarget or event == fk.TargetConfirmed) and data.card.trueName == "slash" and
      data:isOnlyTarget(data.to) and not data.to:isNude() and not data.from.dead and not data.to.dead
  end,
  on_cost = function(self, event, target, player, data)
    local n = player:hasSkill("hanqing__fusheng") and player.kingdom == "qin" and 2 or 1
    if player.room:askToSkillInvoke(data.from, {
      skill_name = zhuini.name,
      prompt = "#hanqing__zhuini-invoke::"..data.to.id..":"..n,
    }) then
      event:setCostData(self, {extra_data = n})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from
    local to = data.to
    local n = event:getCostData(self).extra_data
    local cards = to:getCardIds("he")
    if #cards > n then
      cards = room:askToChooseCards(from, {
        target = to,
        min = n,
        max = n,
        flag = "he",
        skill_name = zhuini.name,
        prompt = "#hanqing__zhuini-show::"..to.id..":"..n,
      })
    end
    to:showCards(cards)
    if to.dead then return end
    cards = table.filter(cards, function (id)
      return table.contains(to:getCardIds("he"), id)
    end)
    if #cards == 0 or from.dead or
      not room:askToSkillInvoke(to, {
        skill_name = zhuini.name,
        prompt = "#hanqing__zhuini-discard::"..from.id,
      }) then
      local ids = table.filter(cards, function (id)
        return table.contains(to:getCardIds("he"), id) and not to:prohibitDiscard(id)
      end)
      if #ids > 0 then
        room:throwCard(ids, zhuini.name, to, to)
      end
      if to.dead then return end
      ids = table.filter(to:getCardIds("he"), function (id)
        return not table.contains(cards, id)
      end)
      if #ids > 0 then
        room:recastCard(ids, to, zhuini.name)
      end
      if from.dead or player.dead then return end
      if room:askToSkillInvoke(to, {
        skill_name = zhuini.name,
        prompt = "#hanqing__zhuini-change::"..player.id,
      }) then
        local kingdoms = {"qin", "chu"}
        table.removeOne(kingdoms, player.kingdom)
        local kingdom = room:askToChoice(player, {
          choices = kingdoms,
          skill_name = zhuini.name,
          prompt = "AskForKingdom",
        })
        if kingdom ~= player.kingdom then
          room:changeKingdom(player, kingdom, true)
        end
      end
    else
      room:moveCardTo(cards, Card.PlayerHand, from, fk.ReasonGive, zhuini.name, nil, true, to)
    end
  end,
})

return zhuini
